package com.lemon.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.cloud.admin.service.SysPostService;
import com.lemon.cloud.admin.service.UserPostService;
import com.lemon.cloud.comm.constants.enums.DelFlagEnum;
import com.lemon.cloud.admin.entity.UserPost;
import com.lemon.cloud.admin.mapper.SysPostMapper;
import com.lemon.cloud.comm.po.SysPost;
import com.lemon.cloud.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Autowired
    private UserPostService userPostService;


    @Override
    public List<SysPost> selectByUserId(Long userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public IPage<SysPost> getPostByPage(Page<SysPost> page, SysPost post) {
        return this.page(page, Wrappers.<SysPost>lambdaQuery().eq(SysPost::getDelFlag, DelFlagEnum.EXIST.getValue()).orderByAsc(SysPost::getSort));
    }

    @Override
    public boolean save(SysPost entity) {
        validPost(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(SysPost entity) {
        validPost(entity);
        return super.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delPost(Long id) {
        //查询用户是否使用改部门
        long count = userPostService.count(new LambdaQueryWrapper<UserPost>().eq(UserPost::getPostId, id));
        Assert.state(count == 0, "无法删除，存在用户使用该岗位");
        this.update(new LambdaUpdateWrapper<SysPost>()
                .eq(SysPost::getId, id)
                .set(SysPost::getDelFlag, DelFlagEnum.DELETE.getValue())
                .set(SysPost::getUpdateBy, SecurityUtils.getUser().getUsername())
                .set(SysPost::getUpdateTime, LocalDateTime.now())
        );
        return Boolean.TRUE;
    }

    private void validPost(SysPost post) {
        //检查名称和code
        Long commName = baseMapper.selectCount(new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getName, post.getName())
                .eq(SysPost::getDelFlag, DelFlagEnum.EXIST.getValue())
                .ne(post.getId() != null, SysPost::getId, post.getId())
        );
        Assert.state(commName == 0, "存在重复名称岗位");

        Long commCode = baseMapper.selectCount(new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getCode, post.getCode())
                .eq(SysPost::getDelFlag, DelFlagEnum.EXIST.getValue())
                .ne(post.getId() != null, SysPost::getId, post.getId())
        );
        Assert.state(commCode == 0, "存在重复编码岗位");
    }
}
